package com.boge.ai.split;


import java.util.*;

public class RecursiveCharacterTextSplitter {

    private final List<String> separators;
    private final int chunkSize;
    private final int chunkOverlap;

    public RecursiveCharacterTextSplitter(int chunkSize, int chunkOverlap, List<String> separators) {
        this.separators = separators;
        this.chunkSize = chunkSize;
        this.chunkOverlap = chunkOverlap;
    }

    public List<String> splitText(String text) {
        List<String> chunks = new ArrayList<>();
        splitRecursive(text, chunks, 0);
        return chunks;
    }

    private void splitRecursive(String text, List<String> chunks, int currentOverlap) {
        if (text.length() <= chunkSize) {
            chunks.add(text);
            return;
        }

        // 尝试每个分隔符
        for (String sep : separators) {
            List<Integer> indices = findAllIndices(text, sep);

            for (int i = indices.size() - 1; i >= 0; i--) {
                int idx = indices.get(i);

                // 找到最近的一个合适位置作为分割点
                if (idx + sep.length() + currentOverlap <= chunkSize) {
                    String firstChunk = text.substring(0, idx + sep.length());
                    String rest = text.substring(idx + sep.length());

                    chunks.add(firstChunk);
                    splitRecursive(rest, chunks, Math.max(0, firstChunk.length() - (chunkSize - chunkOverlap)));
                    return;
                }
            }
        }

        // 如果所有分隔符都无法满足条件，则强制按字符数截断
        chunks.add(text.substring(0, Math.min(chunkSize, text.length())));
        if (text.length() > chunkSize) {
            splitRecursive(text.substring(chunkSize), chunks, Math.max(0, chunkSize - chunkOverlap));
        }
    }

    // 查找所有匹配的分隔符位置
    private List<Integer> findAllIndices(String text, String separator) {
        List<Integer> indices = new ArrayList<>();
        int index = text.indexOf(separator);
        while (index >= 0) {
            indices.add(index);
            index = text.indexOf(separator, index + separator.length());
        }
        return indices;
    }

    public static void main(String[] args) {
        String text = "自然语言处理（NLP），作为计算机科学、人工智能与语言学的交融之地，致力于赋予计算机解析和处理人类语言的能力。在这个领域，机器学习发挥着至关重要的作用。利用多样的算法，机器得以分析、领会乃至创造我们所理解的语言。从机器翻译到情感分析，从自动摘要到实体识别，NLP的应用已遍布各个领域。随着深度学习技术的飞速进步，NLP的精确度与效能均实现了巨大飞跃。如今，部分尖端的NLP系统甚至能够处理复杂的语言理解任务，如问答系统、语音识别和对话系统等。NLP的研究推进不仅优化了人机交流，也对提升机器的自主性和智能水平起到了关键作用。";

        RecursiveCharacterTextSplitter splitter = new RecursiveCharacterTextSplitter(
                50, 10,
                Arrays.asList("\n\n", "\n", "。", "，", " ", "")
        );

        List<String> chunks = splitter.splitText(text);

        for (int i = 0; i < chunks.size(); i++) {
            System.out.printf("块 %d: 长度 %d: %s%n%n", i + 1, chunks.get(i).length(), chunks.get(i));
        }
    }
}